package game.client.net;

import game.common.net.GamePacketHandler;
import game.common.util.Logger;
import game.common.world.Vector;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;

import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.INonBlockingConnection;

public class InGameDataHandler extends GamePacketHandler {

	private Logger log = Logger.getLogger(getClass());
	
	private ObjectPositionListener listener;
	
	public InGameDataHandler(ObjectPositionListener listener) {
		this.listener = listener;
	}

	@Override
	protected boolean readPacket(int type, int len, INonBlockingConnection nbc)throws IOException, BufferUnderflowException,ClosedChannelException, MaxReadSizeExceededException {
		switch(type){
			case PTYPE_UPDATE_POSITION:
				return handleUpdatePosition(nbc);
				
			default:
				log.debug("unkown packet type " + type);
				return false;
		}
	}
	
	private boolean handleUpdatePosition(INonBlockingConnection nbc) throws BufferUnderflowException, IOException{
		String obj = readPlayerName(nbc);
		Vector pos = readVector(nbc);
		
		this.listener.objectMoved(obj, pos);
		return false;
	}
}
